#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 焦点控制
    ui->lineEdit->setFocusPolicy(Qt::NoFocus);// 无法获取焦点
    ui->lineEdit_2->setFocusPolicy(Qt::TabFocus);// Tab获取焦点
    ui->lineEdit_3->setFocusPolicy(Qt::ClickFocus);// 点击获取焦点
    ui->lineEdit_4->setFocusPolicy(Qt::StrongFocus);// Tab或点击获取焦点

    QIcon icon(":/meteor2");
    // 设置按钮图标
    ui->pushButton->setIcon(icon);
    // 设置图标大小
    ui->pushButton->setIconSize(QSize(50,50));

    // 给按钮设置快捷键，推荐第二种方式，Qt中的枚举，错误会报错，第一种输错了不会报错
    //ui->pushButton_light->setShortcut(QKeySequence("Up"));
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_Up));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_Down));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_Left));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_Right));

    // 开启鼠标按住连发(键盘默认是支持的)
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

    // radiobutton默认选中
    ui->radioButton_agree->setChecked(true);
    ui->label->setText("你同意我是帅哥！");
    // radiobutton禁用选项-只能让其无法选中，但是仍然可以响应点击事件
    //ui->radioButton_disagree->setCheckable(false);
    // 所以可以使用下面的方式来彻底将其禁用
    ui->radioButton_disagree->setEnabled(false);
    ui->radioButton_disagree->setDisabled(true);

    // 因为radiobutton默认是排他的，但是可能一个屏幕上有多个按钮需要选择，比如点餐时要选择一个汉堡一个薯条
    // 这是就要对其进行分组
    QButtonGroup* group1 = new QButtonGroup(this);
    QButtonGroup* group2 = new QButtonGroup(this);

    group1->addButton(ui->radioButton);
    group1->addButton(ui->radioButton_2);
    group1->addButton(ui->radioButton_3);
    group1->addButton(ui->radioButton_4);
    group2->addButton(ui->radioButton_5);
    group2->addButton(ui->radioButton_6);
    group2->addButton(ui->radioButton_7);
    group2->addButton(ui->radioButton_8);
}

Widget::~Widget()
{
    delete ui;
}

// 设置控件样式
void Widget::on_pushButton_light_clicked()
{
    // 常用这两种RGB方式来表示颜色，因为可以表示无数种颜色，不使用英文单词
    this->setStyleSheet("background-color: rgb(240,240,240);");

    ui->textEdit->setStyleSheet("background-color:0xFFFFFF;color:0x0;");

    ui->pushButton_light->setStyleSheet("color:balck;");
    ui->pushButton_dark->setStyleSheet("color:balck;");
}

void Widget::on_pushButton_dark_clicked()
{
    this->setStyleSheet("background-color: balck;");

    ui->textEdit->setStyleSheet("background-color:balck;color:white;");

    ui->pushButton_light->setStyleSheet("color:white;");
    ui->pushButton_dark->setStyleSheet("color:white;");
}

// 鼠标、键盘移动控件
void Widget::on_pushButton_up_clicked()
{
    QRect rect = ui->pushButton_pictory->geometry();
    ui->pushButton_pictory->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton_pictory->geometry();
    ui->pushButton_pictory->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton_pictory->geometry();
    ui->pushButton_pictory->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton_pictory->geometry();
    ui->pushButton_pictory->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
}

// radiobutton
void Widget::on_radioButton_agree_clicked()
{
    ui->label->setText("你同意我是帅哥！");
}

void Widget::on_radioButton_sure_agree_clicked()
{
    ui->label->setText("你非常同意我是帅哥！");
}

void Widget::on_radioButton_disagree_clicked()
{
    ui->label->setText("你不同意我是帅哥！");
}

// 测试其他槽函数
// 是否被选中，选中就会触发，相当于按下抬起，一个pressed + 一个released
void Widget::on_radioButton_clicked(bool checked)
{
    qDebug() << "childed:" << checked;
}
// 按下触发
void Widget::on_radioButton_2_pressed()
{
    qDebug() << "pressed";
}
// 抬起触发
void Widget::on_radioButton_3_released()
{
    qDebug() << "released";
}
// 状态改变触发
void Widget::on_radioButton_4_toggled(bool checked)
{
    qDebug() << "toggled";
}

void Widget::on_radioButton_5_clicked()
{
    qDebug() << "childed_5";
}

void Widget::on_radioButton_6_clicked()
{
    qDebug() << "childed_6";
}

void Widget::on_radioButton_7_clicked()
{
    qDebug() << "childed_7";
}

void Widget::on_radioButton_8_clicked()
{
    qDebug() << "childed_8";
}
